Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
get-user-locale
Advanced tools
The get-user-locale npm package is a simple utility that helps you determine the user's locale based on their browser settings. This can be useful for internationalization (i18n) and localization (l10n) purposes in web applications.
Get User Locale
This feature allows you to retrieve the user's locale setting from their browser. The code sample demonstrates how to import the package, get the user's locale, and log it to the console.
const getUserLocale = require('get-user-locale');
const locale = getUserLocale();
console.log(locale);
Get User Locale with Fallback
This feature allows you to specify a fallback locale in case the user's locale cannot be determined. The code sample shows how to set a fallback locale of 'en-US'.
const getUserLocale = require('get-user-locale');
const locale = getUserLocale({ fallbackLocale: 'en-US' });
console.log(locale);
The os-locale package retrieves the locale of the operating system. It is more focused on the system's locale rather than the browser's locale, making it useful for Node.js applications running on a server or desktop environment.
The browser-locale package is similar to get-user-locale in that it retrieves the user's locale from the browser. However, it provides a more straightforward API and does not support fallback locales.
The intl-locales-supported package checks if the current environment supports a list of locales. While it does not retrieve the user's locale, it is useful for ensuring that the necessary locale data is available for internationalization purposes.
A function that returns user's locale as an IETF language tag, based on all available sources.
npm install get-user-locale
or yarn add get-user-locale
.import getUserLocale from 'get-user-locale'
.const userLocale = getUserLocale();
getUserLocale()
A function that returns user's preferred locale as an IETF language tag, based on all available sources.
'de-DE';
import getUserLocale from 'get-user-locale';
or
import { getUserLocale } from 'get-user-locale';
getUserLocale()
may be called with an optional options
argument.
options
object may contain the following properties:
Property | Description | Default value |
---|---|---|
fallbackLocale | A locale to use as a fallback. | en-US |
useFallbackLocale | Whether to use the fallback locale. | true |
getUserLocales()
A function that returns an array of user's preferred locales as an IETF language tags, based on all available sources.
['de-DE', 'de', 'en-US', 'en'];
import { getUserLocales } from 'get-user-locale';
getUserLocales()
may be called with an optional options
argument.
options
object may contain the following properties:
Property | Description | Default value |
---|---|---|
fallbackLocale | A locale to use as a fallback. | en-US |
useFallbackLocale | Whether to use the fallback locale. | true |
There are at least five ways of determining user's locale:
window.navigator.languages
window.navigator.language
window.navigator.userLanguage
window.navigator.browserLanguage
window.navigator.systemLanguage
...languages
is an array of strings, the others are strings. Some browsers return mixed-case IETF language tags (e.g. de-DE
), while others return lowercase ones (e.g. de-de
). Finally, non-browser environments will not return anything, so you need a fallback.
Get-User-Locale does the following:
-
is always uppercased),en-US
, so if all else fails, you will get a result that won't crash your app.The MIT License.
Wojciech Maj kontakt@wojtekmaj.pl https://wojtekmaj.pl |
FAQs
Returns a list of strings representing the user's preferred languages.
The npm package get-user-locale receives a total of 553,426 weekly downloads. As such, get-user-locale popularity was classified as popular.
We found that get-user-locale demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.